<template>
  <div class="container">
    <div style="margin-bottom: 6px">
      <a-space>
        <a-button type="primary" status="success" @click="refreshData">
          <template #icon><icon-refresh /></template>刷新
        </a-button>
        <ImportHome :entityTypeId="1" :key="state.key"></ImportHome>
      </a-space>
    </div>
    <a-table :bordered="{cell:true}" :pagination="false" stripe :data="state.recordObj.list">
      <template #columns>
        <a-table-column title="标题" data-index="cdefine1"></a-table-column>
        <a-table-column title="事业部车间" data-index="orgName"></a-table-column>
        <a-table-column title="填报人" data-index="userName"></a-table-column>
        <a-table-column title="计划生产日期">
          <template #cell="{ record }"><DateFormat :value="record.recordtime" :format="'YYYY-MM-DD'"></DateFormat></template>
        </a-table-column>
        <a-table-column title="操作" width="200">
          <template #cell="{ record }">
            <a-space>
              <a-button type="primary" status="warning" size="small" @click="editScviewRecord(record.id)"><template #icon><icon-edit /></template>填报</a-button>
              <a-popconfirm content="确认删除吗?" @ok="deleteScviewRecord(record.id)">
                <a-button type="primary" status="danger" size="small"><template #icon><icon-delete /></template>删除</a-button>
              </a-popconfirm>
            </a-space>
          </template>
        </a-table-column>
      </template>
    </a-table>
    <a-pagination class="mt10" show-total :total="state.recordObj.total" @change="loadScviewRecord" @page-size-change="loadScviewRecord" v-model:current="state.sd.pageNum" v-model:page-size="state.sd.pageSize"></a-pagination>
  </div>
</template>

<script lang="ts" setup>
import axios from "axios";
import { Message } from '@arco-design/web-vue'
import mittBus from "@/utils/route-listener"
import {reactive, ref, onMounted, onUnmounted, defineAsyncComponent} from "vue";
import { useRouter } from "vue-router";
import {uuid} from "@/utils/util";

const router = useRouter();

const state = reactive({
  sd: {
    pageNum: 1,
    pageSize: 15
  },
  key : 'init1',
  recordObj: {list : [], total : 0},
})
const refreshData = ()=>{
  state.sd.pageNum = 1
  loadScviewRecord();
}
const loadScviewRecord = ()=>{
  axios.post('/scviewrecord/pagescviewrecord', state.sd).then(res => {
    const { data } = res
    state.recordObj = data
  })
}
const editScviewRecord = (id)=>{
  router.push("/cjdata/scviewrecordtb?recordid=" + id)
}
const deleteScviewRecord = (id)=>{
  axios.post('/scviewrecord/deletescviewrecord',{id : id}).then(res => {
    const { code, msg } = res
    if(code === '200') {
      Message.success(msg)
      loadScviewRecord();
    }
  })
}
onMounted(()=>{
  loadScviewRecord();
  mittBus.on("import_bus", (obj)=>{
    if(!obj.fId){
      Message.error("上传文件未知错误，获取文件ID失败")
      return
    }
    axios.post('/scviewrecord/addbyupload',{fileId : obj.fId}).then(res => {
      const { code, msg } = res
      if(code === '200') {
        state.key = uuid()
        Message.success(msg)
        refreshData();
      }
    })
  })
})
onUnmounted(()=>{
  mittBus.off("import_bus", ()=>{})
})
</script>

<style scoped>

</style>
